package com.aliyun.commons.dlock;

import java.util.function.Supplier;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

import lombok.Data;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.validation.annotation.Validated;

/**
 * 分布式锁，会自动续期
 *
 * 
 * @date 2024/11/28
 */
@Validated
public interface DistributedLock {

    /**
     * 加锁执行：自动加锁->执行->解锁，续期；expireSeconds 超时时间必须 >= 10s
     *
     * @param lockKey       加锁key
     * @param expireSeconds 超时时间，单位秒
     * @param action        操作
     * @return {@link Boolean}
     */
    <T> LockResult<T> executeWithLock(@NotEmpty String lockKey, @Min(value = 10) int expireSeconds,
        @NotNull Supplier<T> action);

    @Data
    class LockResult<T> {
        private boolean success;
        private T data;

        public LockResult(boolean success, T data) {
            this.success = success;
            this.data = data;
        }
    }
}